package com.example.a2022application.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.a2022application.BaseActivity;
import com.example.a2022application.R;
import com.example.a2022application.bean.GroupBean;
import com.example.a2022application.bean.IsGroupBean;
import com.example.a2022application.bean.RegisterBean;
import com.example.a2022application.net.Urls;
import com.example.a2022application.utils.SpUtils;
import com.example.a2022application.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;

/**
 * 小组讨论
 */
public class GroupDiscussActivity extends BaseActivity implements View.OnClickListener {
    private ListView listgroup;
    List<GroupBean.DataBean> tList= new ArrayList();
    ShowListAdapter adapter;
    private TextView tv_create;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group);
        listgroup = findViewById(R.id.listgroup);
        tv_create = findViewById(R.id.tv_create);
        tv_create.setOnClickListener(this);

        String cuserid = SpUtils.getString(GroupDiscussActivity.this,"userid");
        OkGo.<String>post(Urls.SELECTISGROUP)
                .params("cuserid",cuserid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        IsGroupBean bean = getGson().fromJson(response.body(),IsGroupBean.class);
                        if (bean.getCode() == 0){
                            if (bean.getData().size()>0){
                                Intent intent = new Intent(GroupDiscussActivity.this,DiscussActivity.class);
                                intent.putExtra("groupName",bean.getData().get(0).getGroup_name());
                                startActivity(intent);
                                finish();
                            }
                        }
                    }
                });
    }

    private void initData() {
        OkGo.<String>post(Urls.SELECTGROUP)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        GroupBean bean = getGson().fromJson(response.body(),GroupBean.class);
                        if (bean.getCode() == 0){
                            if (bean.getData().size()>0){
//                                tv_create.setVisibility(View.GONE);
                                tList = bean.getData();
                                adapter = new ShowListAdapter();
                                listgroup.setAdapter(adapter);
                            }else {
//                                tv_create.setVisibility(View.VISIBLE);
                            }
                        }

                    }
                });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_create:
                 startActivity(new Intent(GroupDiscussActivity.this,CreateGroupActivity.class));
                break;
        }
    }

    public class ShowListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return tList.size();
        }

        @Override
        public Object getItem(int i) {
            return tList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ShowListAdapter.ViewHolder viewHolder;
            //判断界面是否有缓存
            if(convertView == null){
                viewHolder= new ShowListAdapter.ViewHolder();
                convertView= LinearLayout.inflate(parent.getContext(), R.layout.item_group, null);

                viewHolder.tv_name = convertView.findViewById(R.id.tv_name);
                viewHolder.tv_add = convertView.findViewById(R.id.tv_add);

                convertView.setTag(viewHolder);
            }else{
                viewHolder=(ShowListAdapter.ViewHolder)convertView.getTag();
            }
            GroupBean.DataBean group = (GroupBean.DataBean) tList.get(position);

            viewHolder.tv_name.setText(group.getGroup_name());
            viewHolder.tv_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ToastUtil.showToast("点击了加入该组");
                    String userid = SpUtils.getString(GroupDiscussActivity.this,"userid");
                    if (TextUtils.isEmpty(userid)){
                        ToastUtil.showToast("请先登录");
                    }else {
                        OkGo.<String>post(Urls.ADDGROUP)
                                .params("userId",userid)
                                .params("groupName",group.getGroup_name())
                                .params("master","0")
                                .execute(new StringCallback() {
                                    @Override
                                    public void onSuccess(Response<String> response) {
                                        RegisterBean bean = getGson().fromJson(response.body(),RegisterBean.class);
                                        if (bean.getCode() == 0){
                                            Intent intent = new Intent(GroupDiscussActivity.this,DiscussActivity.class);
                                            intent.putExtra("groupName",group.getGroup_name());
                                            startActivity(intent);

                                        }

                                    }
                                });

                    }

                }
            });

            return convertView;
        }

        private class ViewHolder{
            TextView tv_name;
            TextView tv_add;

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }
}
